package com.yy.base.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理类
 * @author 周滔
 */
public class DateTime {
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	private SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private Calendar cal = null;
	public DateTime() {
		cal = Calendar.getInstance();
	}
	public DateTime(Date date) {
		cal = Calendar.getInstance();
		cal.setTime(date);
	}
	public DateTime(String date, String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		cal = Calendar.getInstance();
		cal.setTime(sdf.parse(date));
	}
	public int getYear() {
		return cal.get(Calendar.YEAR);
	}
	public int getMonth() {
		return cal.get(Calendar.MONTH) + 1;
	}
	public int getDay() {
		return cal.get(Calendar.DATE);
	}
	public int getHours() {
		return cal.get(Calendar.HOUR_OF_DAY);
	}
	public int getMinutes() {
		return cal.get(Calendar.MINUTE);
	}
	public int getSeconds() {
		return cal.get(Calendar.SECOND);
	}
	public int getDayOfWeek() {
		return cal.get(Calendar.DAY_OF_WEEK);
	}
	public long getTime() {
		return cal.getTimeInMillis();
	}
	public int getDayOfYear() {
		return cal.get(Calendar.DAY_OF_YEAR);
	}
	
	public void addYear(int year) {
		cal.set(Calendar.YEAR, getYear() + year);
	}
	public void addMonth(int month) {
		cal.set(Calendar.MONTH, getMonth() + month);
	}
	public void addDay(int day) {
		cal.set(Calendar.DATE, getDay() + day);
	}
	public void addHours(int hour) {
		cal.set(Calendar.HOUR, getHours() + hour);
	}
	public void addMinutes(int minutes) {
		cal.set(Calendar.MINUTE, getMinutes() + minutes);
	}
	public void addSeconds(int seconds) {
		cal.set(Calendar.SECOND, getSeconds() + seconds);
	}
	/**
	 * 获取带时分秒的时间字符
	 */
	public String toString() {
		return dateTimeFormat.format(cal.getTime());
	}
	/**
	 * 获取不带时分秒的时间字符 
	 */
	public String toDateString() {
		return dateFormat.format(cal.getTime());
	}
	
	public Date getDate() {
		return cal.getTime();
	}
	
	public static void main(String[] args) {
		DateTime dt = new DateTime();
		dt.addDay(10);
		System.out.println(dt.getDayOfWeek());
		System.out.println(dt.toString());
	}
}